layui.use(['form', 'layer', 'tree'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    function CreateTreeNode(data) {
        var result = [];
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            if (item.Items.length) {
                result.push({
                    name: item.UserName + '(' + item.RealName + ')',
                    spread: true, // 展开
                    children: CreateTreeNode(item.Items)
                });
            } else {
                result.push({
                    name: item.UserName + '(' + item.RealName + ')',
                });
            }
        }
        return result;
    }

    function InitTree() {
        $.ajax({
            type: 'GET',
            url: '/Manager/GetStructureTree/',
            data: {},
            dataType: "json",
            success: function (res) {//res为相应体,function为回调函数
                console.log(res);
                if (res.Success) {
                    var nodes = CreateTreeNode(res.Data);
                    layui.tree({
                        elem: '#demo' //传入元素选择器
                        , nodes: nodes
                    });
                }
                else {
                    layer.alert(res.ResultMsg, { icon: 5 });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

    InitTree();

    //layui.tree({
    //    elem: '#demo' //传入元素选择器
    //    , nodes: [{ //节点
    //        name: '父节点1'
    //        , children: [{
    //            name: '子节点11'
    //        }, {
    //            name: '子节点12'
    //        }]
    //    }, {
    //        name: '父节点2（可以点左侧箭头，也可以双击标题）'
    //        , children: [{
    //            name: '子节点21'
    //            , children: [{
    //                name: '子节点211'
    //            }]
    //        }]
    //    }]
    //});

});